package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Example02 {
    public static void main(String[] args) {
        JButton btn1=new JButton("模态对话框");
        JButton btn2=new JButton("非模态对话框");
        JFrame f=new JFrame("DialogDemo");
        f.setSize(300,250);
        f.setLocation(300,200);
        f.setLayout(new FlowLayout());
        f.add(btn1);
        f.add(btn2);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        JLabel label=new JLabel();
        JDialog dialog=new JDialog(f,"Dialog");
        dialog.setSize(220,150);
        dialog.setLocation(350,250);
        dialog.setLayout(new FlowLayout());
        JButton btn3=new JButton("确定");
        dialog.add(btn3);
        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.setModal(true);
                if(dialog.getComponents().length==1){
                    dialog.add(label);
                }
                label.setText("非模式对话框,点击确定按钮关闭");
                dialog.setVisible(true);
            }
        });
        btn3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        });
        btn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.setModal(false);
                if(dialog.getComponents().length==1){
                    dialog.add(label);
                }
                label.setText("非模式对话框,点击确定按钮关闭");
                dialog.setVisible(true);
            }
        });
    }
}
